<?php
class FillAction extends CommonAction {
    private $Fill;

	public function __construct(){
		parent::__construct();
		$this->checkAuth();
	    $this->Fill = D('Fill');
	}
	//空操作
	Public function _empty(){
	    header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码
	    $this->display("Public:404");
	}
	public function text(){
	
		$tel="15804974507";
		$num=rand(111111, 999999);
		$neirong="您本次的验证码是：".$num."请不要透露给他人"."【".$this->sys['title']."】";
		$this->tel_fasong($tel, $neirong,$num);
	}
    public function all(){
        $per_num = 10;
        $page = chkNum($_GET['page']) ? $_GET['page'] : 1;
		$count = $this->Fill->where('uid='.$this->auth['id'].' and status=1')->count();
        $page_num = ceil($count/$per_num);
        if($page > $page_num) $page = $page_num;

        $list=$this->Fill->where('uid='.$this->auth['id'].' and status=1')->order('ctime desc')->limit(($page-1)*$per_num.','.$per_num)->select();

        $this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('page_num',$page_num);
		$this->assign('mod','list');
		$this->assign('empty','<tr><td colspan="2" style="text-align:center">没有找到数据！</td></tr>');

		$this->display('./Tpl/Home/Fill_list.html');
    }

    public function index(){
        $per_num = 10;
        $page = chkNum($_GET['page']) ? $_GET['page'] : 1;
		$count = $this->Fill->where('uid='.$this->auth['id'])->count();
        $page_num = ceil($count/$per_num);
        if($page > $page_num) $page = $page_num;

        $list=$this->Fill->where('uid='.$this->auth['id'])->order('ctime desc')->limit(($page-1)*$per_num.','.$per_num)->select();

        $this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('page_num',$page_num);
		$this->assign('mod','list');
		$this->assign('empty','<tr><td colspan="2" style="text-align:center">没有找到数据！</td></tr>');
	
		$this->display('./Tpl/Home/Fill.html');
    }

	public function add(){
		$id=intval($_GET['id']);
		if(!empty($id)){
		$this->assign("id",$id);
		}
	    if (IS_POST){
	        $data['num']=$_POST['num']+$_POST['random'];
	        $data['random']=rand(0001, 9999);
	        $data['uid']=$_SESSION['USER_KEY_ID'];
	        $data['ctime']=time();
	        $data['tradeno']='Apay'.time();
	        $data['status']=0;
	        $data['bank']=$_POST['bank'];
	        $data['type']=1;
	        $r=M('Fill')->add($data);
	        if ($r){
	            $arr=array("status"=>1,'info'=>'成功','id'=>$r);
	            $this->ajaxReturn($arr);
	        }else {
	        	
	            $arr=array('status'=>0,'info'=>'提交失败');
	            $this->ajaxReturn($arr);
	        }
	    }else {
    	    $this->assign('random','0.'.rand(0, 1).rand(1, 9));
    	    //获取打款银行信息
			
    	 
    	    $bank_ad=M('Bank_admin')->field("t_bank_admin.*,t_fill.num,t_fill.random")->where("t_fill.id='$id'")->join("t_fill on t_bank_admin.id=t_fill.bank")->find();
    	   
    	    $bank=M("Bank_admin")->select();
    	    $this->assign('bank',$bank);
    	    $this->assign('bank_ad',$bank_ad);
    	    
    	    $per_num = 10;
    	    $where['type']=1;
    	    $where['uid']=$this->auth['id'];
    	    $page = chkNum($_GET['page']) ? $_GET['page'] : 1;
    	    $count = $this->Fill->where($where)->count();
    	    $page_num = ceil($count/$per_num) > 0 ? ceil($count/$per_num) : 1;
    	    if($page > $page_num) $page = $page_num;
    	    
    	    $list = $this->Fill->where($where)->order('ctime desc')->limit(($page-1)*$per_num.','.$per_num)->select();
    	  	foreach ($list as $k=>$v){
    	  		$list[$k]['status_fill']=$this->status_fill($v['status']);
    	  	}
    	    $this->assign('page',$page);
    	    $this->assign('page_num',$page_num);
    	    $this->assign("list",$list);
    	    $this->assign('empty','<tr><td colspan=2>您还没有认购记录！</td>');
    	    
    	    $user=M("User")->where("id='{$_SESSION['USER_KEY_ID']}'")->find();
    	    $this->assign("user",$user);
    	    $this->display('./Tpl/Home/Fill.html');
	    }
	}
	private function status_fill($status){
		switch ($status){
			case 0:$name="尚未付款";break;
			case 1:$name="等待银行到账";break;
			case 2:$name="交易成功";break;
			case -1:$name="用户撤销";break;
			default:$name="未知状态";
		}
		return $name;
	}
	public function zhifu_add(){
		$where['uid']=$this->auth['id'];
		$where['type']=2;
		$per_num = 10;
		$page = chkNum($_GET['page']) ? $_GET['page'] : 1;
		$count = $this->Fill->where($where)->count();
		$page_num = ceil($count/$per_num) > 0 ? ceil($count/$per_num) : 1;
		if($page > $page_num) $page = $page_num;
			
		$list = $this->Fill->where($where)->order('ctime desc')->limit(($page-1)*$per_num.','.$per_num)->select();
		foreach ($list as $k=>$v){
			$list[$k]['status_fill']=$this->status_fill($v['status']);
		}
		$this->assign('page',$page);
		$this->assign('page_num',$page_num);
		$this->assign("list",$list);
		$this->assign('empty','<tr><td colspan=2>您还没有认购记录！</td>');
		$num=rand(0001,9999);
		$this->assign("num",$num);
		$art=M("Art")->where("title='充值说明'")->find();
		$this->assign("art",$art);
		$this->display('./Tpl/Home/Fill_zhifu.html');
	}
	public function zhifu_add_ok(){
			if(empty($_POST['money'])){
				$this->error("请输入金额");
			}
			$data['num']=$_POST['money'];
	        $data['random']=$_POST['title'];
	        $data['uid']=$_SESSION['USER_KEY_ID'];
	        $data['ctime']=time();
	        $data['tradeno']='Apay'.time();
	        $data['status']=0;
			$data['type']=2;
			$data['banknum']='';
			$data['bankname']="支付宝";
	        $r=M('Fill')->add($data);
	        if($r){
	            
	           
	            $fill=M("Fill")->where("id='$r'")->find();
	            $this->assign("fill",$fill);
	            $this->display('./Tpl/Home/Fill_jump.html');

	        }else{
	        	$this->error("充值失败");
	        }
	}

	public function quxiao(){
		$id=intval($_GET['id']);
		$fill=M("Fill")->where("uid='{$_SESSION['USER_KEY_ID']}' and id ='$id' and status = 0")->find();
		if(empty($fill)){
			$this->error("非本人不可操作");
		}
		M("Fill")->where("id='$id'")->setField("status",-1);
		$this->success("已经取消");
	}
	
	public function add_fill(){
		$id=$_POST['id'];
		$fill=M("Fill")->where("id='$id' and uid='{$_SESSION['USER_KEY_ID']}'")->find();
		if(empty($fill)){
			$this->error("无效信息");
		}
		if($fill['status']!=0){
			$this->error("已经操作，请勿重复操作");
		}
		
		if(empty($_POST['bankname'])){
			$this->error("请填写您的汇出银行");
		}
		if(empty($_POST['banknum'])){
			$this->error("请填写银行账号");
		}
		if(empty($_POST['uname'])){
			$this->error("请填写打款人姓名");
		}
		if(empty($_POST['tel'])){
			$this->error("请填写电话号码");
		}
		$data['bankname']=$_POST['bankname'];
		$data['banknum']=$_POST['banknum'];
		$data['uname']=$_POST['uname'];
		$data['tel']=$_POST['tel'];
		$data['status']=1;
		
		$re=M("Fill")->where("id='$id'")->save($data);
		if($re){
			$this->success("补全信息成功");
		}else{
			$this->error("补全信息失败");
		}

	}
	public function insert(){
        $rmb = trim($_POST['money']);
        if(!chkNum($rmb)) $this->error('请输入充值金额！');

        $mo = new Model();
		$mo->query('set autocommit=0');
		$mo->query('lock tables t_user write,t_fill write');
		$rs = array();
		$tradeno='Apay'.time();
		$rs[] = $mo->table('t_fill')->add(array(
			'uid'=>$this->auth['id'],
			'num'=>$rmb,
		    'tradeno'=>$tradeno,
			'ctime'=>time()
		));

		if(chkArr($rs)){
			$mo->query('commit');
		    $mo->query('unlock tables');
		    $this->assign('money',$rmb);
		    $this->assign('tradeno',$tradeno);
		    $this->display("./Tpl/Home/payto.html");
		  //  $this->success('充值成功！');
		}else{
			$mo->query('rollback');
		    $this->error('充值失败!');
		}
    }

	//##########
	public function insert2($money,$order_no,$type){
	// $money="0.01";
	// $order_no="Apay1411195123";
	//
		$num = $money;
        if(!chkNum($num)) {
			$this->error('请输入充值金额！');
            exit;
        }

        $mo = new Model();
		$mo->query('set autocommit=0');
		$mo->query('lock tables t_fill write,t_user write');
		$rs = array();
		$mo->table('t_fill').
        $fill = $mo->table('t_fill')->where('tradeno=\''.$order_no.'\'')->find();

		if($fill['status']==1){
		    $mo->query('rollback');
			if($type!="notify"){
				$this->error('已经充值了！','/?s=Home/Fill');
			}
			exit;
		}
		$id=$fill['id'];
		$uid=$fill['uid'];

		$rs[] = $mo->table('t_fill')->save(array(
			'id'=>$id,
			'status'=>1
		));

		$rs[] = $mo->table('t_user')->save(array(
			'id'=>$uid,
			'rmb'=>array('exp','rmb+'.$num)
		));
		
		if(chkArr($rs)){
			$mo->query('commit');
		    $mo->query('unlock tables');
			if($type!="notify"){
				$this->success('充值成功！','/?s=Home/Fill');
			}
		}else{
			$mo->query('rollback');
			if($type!="notify"){
				$this->error('充值失败！');
			}
		}
		
    }
	
	public function urlreturn(){
		$order_no = $_REQUEST["order_no"];
		$order_amount = $_REQUEST["order_amount"];
		$order_amount=number_format($order_amount,2,".","");
		$return=$_REQUEST["return"]; //return
		$sign=$_REQUEST["sign"];
		$md5key="a59068da06d57e2a8a49a65c7abec098";
		
		$signstr="order_no=".$order_no."&order_amount=".$order_amount."&return=".$return.$md5key;
		$signnew=md5($signstr);
		if($sign==$signnew){
			$this->insert2($order_amount,$order_no,$return);
		}else{
			echo "error";
		}
    }
	
    public function subdata(){
	    $num = trim($_POST['num']);
		$payt = trim($_POST['payt']);
		$bankid=trim(isset($_POST['bankid'])?$_POST['bankid']:"");
        if(!chkNum($num)) {echo '<script language="javascript">history.back();</script>';exit;}

        $mo = new Model();
		$mo->query('set autocommit=0');
		$mo->query('lock tables t_fill write');
		$rs = array();

		$tradeno = "Apay".time();
		$alipayuer = $this->sys['alipay'];
		
		$rs[] = $mo->table('t_fill')->add(array(
			'uid'=>$this->auth['id'],
			'num'=>$num,
			'tradeno'=>$tradeno,
			'ctime'=>time()
		));
		
		if(chkArr($rs)){
			$mo->query('commit');
		    $mo->query('unlock tables');
			$this->assign('num',$num);
			$this->assign('tradeno',$tradeno);
			$this->assign('alipayuer',$alipayuer);
			$this->assign('cellphone',$cellphone);
			if ($payt==1){
				echo "<script>location.href='http://shop.ykjfy.com/mobaopay/req.php?orderno=".$tradeno."&amount=".$num."&bankid=".$bankid."';</script>";
				exit();
				//$this->display('./mobaopay/Payw.html');
				//$this->display('./Tpl/Home/Payw.html');
			}else{
			    $this->display('./Tpl/Home/Pay.html');
			   }
			
		}else{
			$mo->query('rollback');
			echo '<script language="javascript">history.back();</script>';
		}
	}

	public function returndata(){
        $tradeno=$_GET['name'];  //软件上的[用户名] 支付宝交易中的[付款说明]  相当于网站上的充值订单
		$key=trim($_GET['key']);//接口上的key 与后台key相等后才进行逻辑处理　保证安全
		$money=$_GET['money'];//相当于交易中的支付金额

		if($key!=$this->sys['alipaypwd']){echo '0';exit();}

        $mo = new Model();
		$mo->query('set autocommit=0');
		$mo->query('lock tables t_user write,t_fill write');
		$rs = array();

		$fill = $mo->table('t_fill')->where('tradeno="'.$tradeno.'"')->find();
		if(!$fill || $fill['status']==1){echo '0';exit();}
		
		$rs[] = $mo->table('t_fill')->save(array(
			'uid'=>$fill['id'],
			'status'=>1
		));
		$rs[] = $mo->table('t_user')->save(array(
		    'id'=>$fill['uid'],
			'rmb'=>array('exp','rmb+'.$money)
		));

		if(chkArr($rs)){
			$mo->query('commit');
		    $mo->query('unlock tables');
			echo '1';exit();
		}else{
			$mo->query('rollback');
		    echo '0';exit();
		}
    }
}